package com.aptech.yuanhuabao.ui.start.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.aptech.yuanhuabao.R;
import com.aptech.yuanhuabao.ui.main.activity.MainActivity;
import com.aptech.yuanhuabao.ui.start.contract.LoginContract;
import com.aptech.yuanhuabao.ui.start.model.LoginModel;
import com.aptech.yuanhuabao.ui.start.presenter.LoginPresenter;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.commonutils.KeyBordUtil;
import com.jaydenxiao.common.commonwidget.TipDialog;

import butterknife.BindView;
import butterknife.OnClick;


public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements LoginContract.View {


    @BindView(R.id.et_login_account)
    EditText mEtLoginAccount;
    @BindView(R.id.img_empty_account)
    ImageButton mImgEmptyAccount;
    @BindView(R.id.et_login_pwd)
    EditText mEtLoginPwd;
    @BindView(R.id.imag_empty_pwd)
    ImageButton mImagEmpty;
    @BindView(R.id.imag_pwd_eye)
    ImageButton mImagPwdEye;
    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.tv_registered)
    TextView mTvRegistered;
    @BindView(R.id.view)
    View mView;
    @BindView(R.id.tv_forget_pwd)
    TextView mTvForgetPwd;
    @BindView(R.id.tv_twodimensioncode_login)
    TextView mTvTwodimensioncodeLogin;
    @BindView(R.id.tv_test)
    TextView mTvTest;
    @BindView(R.id.tv_formal)
    TextView mTvFormal;
    @BindView(R.id.img_logo)
    ImageView mImgLogo;

    private boolean mbDisplayFlg;

    /**
     * 入口
     *
     * @param activity
     */
    public static void startAction(Activity activity) {

        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {

        mEtLoginAccount.addTextChangedListener(loginTextListener);
        mEtLoginPwd.addTextChangedListener(loginPwdTextListener);
    }

    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }


    @OnClick({R.id.img_empty_account,
            R.id.imag_empty_pwd,
            R.id.imag_pwd_eye,
            R.id.btn_login,
            R.id.tv_registered,
            R.id.tv_forget_pwd,
            R.id.tv_twodimensioncode_login,
            R.id.img_logo,
            R.id.tv_test, R.id.tv_formal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_empty_account:// 清空账户
                mEtLoginAccount.setText("");
                break;
            case R.id.imag_empty_pwd:
                mEtLoginPwd.setText("");
                break;
            case R.id.imag_pwd_eye:
                actionPwdEye();
                break;
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_registered:
                break;
            case R.id.tv_forget_pwd:
                break;
            case R.id.tv_twodimensioncode_login:
                break;
            case R.id.img_logo:
                break;
            case R.id.tv_test:
                break;
            case R.id.tv_formal:
                break;
        }
    }

    private void actionPwdEye() {
        if (!mbDisplayFlg) {
            mEtLoginPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            mEtLoginPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        mbDisplayFlg = !mbDisplayFlg;
        mEtLoginPwd.postInvalidate();
        Editable textEditable = mEtLoginPwd.getText();
        Spannable spannable = textEditable;
        Selection.setSelection(spannable, textEditable.length());
    }

    private void login() {
        KeyBordUtil.hideSoftKeyboard(mEtLoginAccount);
        KeyBordUtil.hideSoftKeyboard(mEtLoginPwd);
        mEtLoginAccount.setText("950138006447");
        mEtLoginPwd.setText("95E4D797733B64D5683DA21AD960B752");
        if (TextUtils.isEmpty(mEtLoginAccount.getText())
                || TextUtils.isEmpty(mEtLoginPwd.getText())) {
            TipDialog.show(this, "提示", getString(R.string.login_account_pwd_empty));
        } else {
            mPresenter.loginCenter(mEtLoginAccount.getText().toString().trim(),
                    mEtLoginPwd.getText().toString().trim());
        }
    }

    TextWatcher loginTextListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            String filterStr = editable.toString().trim();
            if (TextUtils.isEmpty(filterStr)) {
                mImgEmptyAccount.setVisibility(View.GONE);
            } else {
                mImgEmptyAccount.setVisibility(View.VISIBLE);
            }

        }
    };

    TextWatcher loginPwdTextListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            String filterStr = editable.toString().trim();
            if (TextUtils.isEmpty(filterStr)) {
                mImagEmpty.setVisibility(View.GONE);
                mImagPwdEye.setVisibility(View.GONE);
            } else {
                mImagEmpty.setVisibility(View.VISIBLE);
                mImagPwdEye.setVisibility(View.VISIBLE);
            }
        }
    };


    @Override
    public void getLoginResult(String result, boolean isSuccess) {
        if (isSuccess) {
            MainActivity.startAction(this);
        }

    }
}
